package org.pinda.gui;

import java.io.IOException;

import org.johnnei.engine.render.ColorHelper;
import org.johnnei.engine.render.TextRenderer;
import org.pinda.PindaConfig;
import org.pinda.server.game.GameServer;

public class CreateRoomGui extends Gui {
	
	private GuiTextBox roomName;
	private GuiFilteredTextBox roomPort;
	private GuiTextBox maxPlayers;
	
	private GuiButton btnHost;
	private GuiButton btnCancel;
	
	private MultiplayerBrowserGui browserGui;
	
	public CreateRoomGui(Gui parent, MultiplayerBrowserGui browserGui) {
		super(parent, 0);
		
		this.browserGui = browserGui;
		
		roomName = new GuiTextBox(this, "Pinda Room", 									  getCenterX() - 100, 100, 200, 20, new ColorHelper(0x00, 0xAA, 0xFF), null);
		maxPlayers = new GuiTextBox(this, "2", 											  getCenterX() - 100, 130, 200, 20, new ColorHelper(0x00, 0xAA, 0xFF), null);
		roomPort = new GuiFilteredTextBox(this, PindaConfig.getInstance().getString("game_port"), getCenterX() - 100, 160, 200, 20, new ColorHelper(0x00, 0xAA, 0xFF), null);
		roomPort.addAcceptableRange(48, 57);
		
		btnHost = new GuiButton(this, getCenterX() - 100, 190, 110, 20, true, "Create Room", new ColorHelper(0x33, 0x33, 0x33), null);
		btnCancel = new GuiButton(this, getCenterX() + 20, 190, 80, 20, true, "Cancel", new ColorHelper(0x33, 0x33, 0x33), null);
		
		addComponent(roomName);
		addComponent(roomPort);
		addComponent(maxPlayers);
		addComponent(btnHost);
		addComponent(btnCancel);
	}
	
	@Override
	public void onTick(int deltaMs) {
		super.onTick(deltaMs);
		if(btnCancel.isClicked()) {
			switchGui(browserGui);
		} else if (btnHost.isClicked()) {
			try {
				GameServer gameServer = new GameServer(Integer.parseInt(roomPort.getText()));
				switchGui(new ServerRoomGui(getParent(), null));
			} catch (IOException e) {
				
			}
		}
	}
	
	@Override
	public void render() {
		super.render();
		//Title
		TextRenderer.drawCentered(getCenterX(), 50, "Create Room");
		//Textbox Titles
		TextRenderer.draw(getCenterX() - 220, 100 + GuiButton.PADDING, "Room Title");
		TextRenderer.draw(getCenterX() - 220, 130 + GuiButton.PADDING, "Max. Players");
		TextRenderer.draw(getCenterX() - 220, 160 + GuiButton.PADDING, "Server Port");
	}

}
